package net.rgehan.Cloth;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class PluginProperties extends Properties {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String fileName, dir;
	FileWriter writer;
	FileReader reader;
	private String info = "You just have to set the name you want for each color\n"
							+"You can also set the translations of each error or information message.\n"
							+"Please notice that in the \"wrong right\" message, the color name is inserted in <color>.";

	public PluginProperties(String file, String dir) {
		this.fileName = file;
		this.dir = dir;
	}

	public void load() {
		File file = new File(this.fileName);
		if (file.exists()) 
		{
			try {
				load(new FileInputStream(this.fileName));
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		else {
			File directory = new File(dir);
			if(!directory.exists()) directory.mkdirs();
			getString("white", "white");
			getString("orange", "orange");
			getString("magenta", "magenta");
			getString("light_blue", "light_blue");
			getString("yellow", "yellow");
			getString("light_green", "light_green");
			getString("pink", "pink");
			getString("gray", "gray");
			getString("light_gray", "light_gray");
			getString("cyan", "cyan");
			getString("purple", "purple");
			getString("blue", "blue");
			getString("brown", "brown");
			getString("dark_green", "dark_green");
			getString("red", "red");
			getString("black", "black");
			getString("right", "You haven't got the right to use /cloth !");
			getString("info", "You just received a stack of <color> wool !");
		}
	}	

	public void save() {
		try {
			store(new FileOutputStream(this.fileName), info);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public int getInteger(String key, int value) {
		if (containsKey(key)) {
			return Integer.parseInt(getProperty(key));
		}

		put(key, String.valueOf(value));
		return value;
	}

	public String getString(String key, String value) {
		if (containsKey(key)) {
			return getProperty(key);
		}

		put(key, value);
		return value;
	}

	public boolean getBoolean(String key, boolean value) {
		if (containsKey(key)) {
			String boolString = getProperty(key);
			return (boolString.length() > 0)
					&& (boolString.toLowerCase().charAt(0) == 't');
		}
		put(key, value ? "true" : "false");
		return value;
	}

}